/* _928CODES_C_
 * 
 * COPYRIGHT 2008/2009 JOHN LEIDEL 
 * 
 * JOHN.LEIDEL@GMAIL.COM
 * 
 * THANKS TO www.928s4vr.com FOR OPTION CODE DECODING
 * http://www.928s4vr.com/codes.htm
 *
 * THANKS TO http://www.928gt.com/t-928paint.aspx FOR PAINT CODE DECODING
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef _928CODES_VERSION_
#define _928CODES_VERSION_ "0.3BETA"
#endif


//--------------------------------------HELP
// HELP
//------------------------------------------
int help()
{
    printf( "\n\n" );
    printf( "%s\n", "============================================" );
    printf( "%s\n", "               928CODES HELP" );
    printf( "%s\n", "============================================" );
    printf( "%s\n", "AT THE MENU PROMPT, YOU HAVE FOUR OPTIONS" );
    printf( "%s\n", "- 0 WILL QUIT 928CODES" );
    printf( "%s\n", "- 1 WILL FURTHER PROMPT YOU FOR A VALID VIN NUMBER AND SUBSEQUENTLY DECODE IT" );
    printf( "%s\n", "- 2 WILL PROMPT YOU FOR EITHER AN OPTION CODE OR A PATH TO AN OPTION FILE" );
    printf( "%s\n", "    OPTION FILES ARE SIMPLY TEXT FILES THAT CONTAIN ONE OPTION CODE PER LINE" );
    printf( "%s\n", "    EX : " );
    printf( "%s\n", "    # BEGIN OPTION FILE" );
    printf( "%s\n", "    009" );
    printf( "%s\n", "    153" );
    printf( "%s\n", "    215" );
    printf( "%s\n", "    # END OPTION FILE");
    printf( "%s\n", "- 3 WILL FURTHER PROMPT YOU FOR THE YEAR AND SUBSEQUENT PAINT CODE" );
    printf( "%s\n", "- 4 WILL DUMP ALL OPTION CODES TO THE SCREEN" );
    printf( "%s\n", "- 5 PRINTS THIS HELP PROMPT" );
    printf( "%s\n\n", "============================================" );

    return 0; 
}

//--------------------------------------DECODE_PAINT
// DECODE_PAINT
//--------------------------------------------------
int decode_paint()
{
    /* VARS */
    char year[20];
    char code[20];
    char *p;
    int myyear = 0;
    /* ---- */

    /* GET THE YEAR */
    printf( "%s", "...PLEASE ENTER THE FOUR DIGIT YEAR [19XX] ==> " );
    fgets( year, sizeof(year), stdin );

    /* REMOVE THE NEWLINE */
    if( (p=strchr( year, '\n' ) ) != NULL )
    {
	*p = '\0';
    }

    if( strlen( year ) < 4 )
    {
	printf( "%s\n", "ERROR! : INVALID YEAR!!" );
	memset( year, 0, sizeof( year ) );
	return -1;
    }

    /* CONVERT TO INTEGER */
    myyear = atoi( year );

    /* SANITY CHECK */
    if( (myyear>1995) || (myyear<1978) )
    {
	printf( "%s\n", "ERROR! : INVALID YEAR!!" );
	memset( year, 0, sizeof( year ) );
	return -1;
    }

    /* GET THE CODE */
    printf( "%s", "...PLEASE ENTER THE COLOR CODE [NO SPACES] ==> " );
    fgets( code, sizeof(code), stdin );

    if( (p=strchr( code, '\n' ) ) != NULL )
    {
	*p = '\0';
    }

    if( strlen( code ) < 2 )
    {
	printf( "%s\n", "ERROR! : INVALID CODE!!" );
	memset( year, 0, sizeof( year ) );
	memset( code, 0, sizeof( code ) );
	return -1;
    }

   
    /* DECODE IT */
    paint_switcher( myyear, code );
        
    memset( year, 0, sizeof( year ) );
    memset( code, 0, sizeof( code ) );
    
    return 0;
}

//--------------------------------------READ_OPT_FILE
// READ_OPT_FILE
//---------------------------------------------------
int read_opt_file( char *opt )
{
    /* VARS */
    FILE *infile = NULL;
    char myopt[20];
    char *p;
    /* ---- */

    infile = fopen( opt, "r" );
    if( infile == NULL )
    {
#ifdef _928LIB_
	printf( "%s%s%s\n", "ERROR : COULD NOT OPEN FILE : ", opt, "<BR>" );
#else
	printf( "%s%s\n", "ERROR : COULD NOT OPEN FILE : ", opt );
#endif
	return -1;
    }

    memset( myopt, 0, sizeof( myopt ) );

    while( !feof( infile ) )
    {
	fgets( myopt, sizeof( myopt ), infile );
	/* REMOVE THE NEWLINE */
	if( (p=strchr( myopt, '\n' ) ) != NULL )
	{
	    *p = '\0';
	}

	options_switcher( myopt );
	
	memset( myopt, 0, sizeof( myopt ) );
    }

    fclose( infile );
    infile = NULL;

    return 0;
}



//--------------------------------------DECODE_OPTIONS
// DECODE_OPTIONS
//----------------------------------------------------
int decode_options()
{
    /* VARS */
    char opt[1024];
    char *p;
    /* ---- */
    
    memset( opt, 0, sizeof( opt ) );
    
    printf( "%s\n", "PLEASE ENTER AN OPTION CODE OR A PATH TO AN OPTION CODE FILE" );
    printf( "%s\n", "[option code files have multiple codes in the file, one per line]" );
    printf( "%s", "===>" );

    fgets( opt, sizeof(opt), stdin );

    printf( "\n" );

    if( strlen( opt ) < 3 )
    {
	printf( "%s\n", "ERROR! : INVALID OPTION!!" );
	return -1;
    }

    /* REMOVE THE NEWLINE */
    if( (p=strchr( opt, '\n' ) ) != NULL )
    {
	*p = '\0';
    }
   
    
    /* CHECK FOR FILE OR SINGLE OPTION */
    if( (opt[0]=='0') || 
	(opt[0]=='1') || 
	(opt[0]=='2') || 
	(opt[0]=='3') || 
	(opt[0]=='4') ||
	(opt[0]=='5') ||
	(opt[0]=='6') ||
	(opt[0]=='7') ||
	(opt[0]=='8') ||
	(opt[0]=='9') ||
	(opt[0]=='C') ||
	(opt[0]=='P') ||
	(opt[0]=='R') ||
	(opt[0]=='X') ||
	(opt[0]=='Z') )
    {
	/* CHECK SECOND LEVEL */
	/* -- DENOTES A WINDOWS PATHNAME, EX C: */
	if( opt[1] != ':' )   
	{
	    /* THIS IS AN OPTION  */
	    options_switcher( opt );
	}
	else 
	{
	    /* THIS IS A PATHNAME */
	    read_opt_file( opt );
	}
	
    }
    else
    {
	read_opt_file( opt );
    }
    /* DONE */

    return 0;
}

//--------------------------------------CHECK_YEAR
// CHECK_YEAR
//------------------------------------------------
int check_year( char vin_year )
{
#ifdef _928LIB_
    switch( vin_year )
    {
	case 'A' :
	    printf( "%s\n", "YEAR    : 1980<BR>" );
	    break;
	case 'B' :
	    printf( "%s\n", "YEAR    : 1981<BR>" );
	    break;
	case 'C' :
	    printf( "%s\n", "YEAR    : 1982<BR>" );
	    break;
	case 'D' :
	    printf( "%s\n", "YEAR    : 1983<BR>" );
	    break;
	case 'E' :
	    printf( "%s\n", "YEAR    : 1984<BR>" );
	    break;
	case 'F' :
	    printf( "%s\n", "YEAR    : 1985<BR>" );
	    break;
	case 'G' :
	    printf( "%s\n", "YEAR    : 1986<BR>" );
	    break;
	case 'H' :
	    printf( "%s\n", "YEAR    : 1987<BR>" );
	    break;
	case 'J' :
	    printf( "%s\n", "YEAR    : 1988<BR>" );
	    break;
	case 'K' :
	    printf( "%s\n", "YEAR    : 1989<BR>" );
	    break;
	case 'L' :
	    printf( "%s\n", "YEAR    : 1990<BR>" );
	    break;
	case 'M' :
	    printf( "%s\n", "YEAR    : 1991<BR>" );
	    break;
	case 'N' :
	    printf( "%s\n", "YEAR    : 1992<BR>" );
	    break;
	case 'P' :
	    printf( "%s\n", "YEAR    : 1993<BR>" );
	    break;
	case 'R' :
	    printf( "%s\n", "YEAR    : 1994<BR>" );
	    break;
	case 'S' :
	    printf( "%s\n", "YEAR    : 1995<BR>" );
	    break;
	default : 
	    break;
    }
#else
    switch( vin_year )
    {
	case 'A' :
	    printf( "%s\n", "YEAR    : 1980" );
	    break;
	case 'B' :
	    printf( "%s\n", "YEAR    : 1981" );
	    break;
	case 'C' :
	    printf( "%s\n", "YEAR    : 1982" );
	    break;
	case 'D' :
	    printf( "%s\n", "YEAR    : 1983" );
	    break;
	case 'E' :
	    printf( "%s\n", "YEAR    : 1984" );
	    break;
	case 'F' :
	    printf( "%s\n", "YEAR    : 1985" );
	    break;
	case 'G' :
	    printf( "%s\n", "YEAR    : 1986" );
	    break;
	case 'H' :
	    printf( "%s\n", "YEAR    : 1987" );
	    break;
	case 'J' :
	    printf( "%s\n", "YEAR    : 1988" );
	    break;
	case 'K' :
	    printf( "%s\n", "YEAR    : 1989" );
	    break;
	case 'L' :
	    printf( "%s\n", "YEAR    : 1990" );
	    break;
	case 'M' :
	    printf( "%s\n", "YEAR    : 1991" );
	    break;
	case 'N' :
	    printf( "%s\n", "YEAR    : 1992" );
	    break;
	case 'P' :
	    printf( "%s\n", "YEAR    : 1993" );
	    break;
	case 'R' :
	    printf( "%s\n", "YEAR    : 1994" );
	    break;
	case 'S' :
	    printf( "%s\n", "YEAR    : 1995" );
	    break;
	default : 
	    break;
    }
#endif
}

//--------------------------------------POST81VIN
// POST81VIN
//-----------------------------------------------
int post81vin( char *vin )
{
#ifdef _928LIB_
    printf( "%s\n", ".........1981-1995 928<BR>" );
    printf( "%s\n", "*************************************************<BR>" );
    printf( "%s%s%s\n", "VIN     : ", vin, "<BR>" );

    if( vin[3] == 'Z' )
    {
	printf( "%s\n", "MODEL   : EURO <BR>" );
    }
    else
    {
	printf( "%s\n", "MODEL   : ROW <BR>" );
	printf( "%s%c%c%c%s\n", "VDS     : ", vin[3], vin[4], vin[5], "<BR>" );
    }

    if( (vin[6]!='9') || (vin[7]!='2') )
    {
	printf( "%s\n", "...OOPS!! FOUND AN INVALID VIN!<BR>" );
	return 0;
    }
    else
    {
	printf( "%s\n", "MODEL   : 928 <BR>" );
    }

    if( (vin[9]=='I') || (vin[9]=='O') || (vin[9]=='Q'))
    {
	printf( "%s\n", "...OOPS!! FOUND AN INVALID VIN!<BR>" );
	return 0;
    }
    else
    {
	check_year( vin[9] );
    }

    printf( "%s\n", "ORIGIN  : STUTTGART<BR>" );
    
    /* TODO : READ vin[12] AS CODE FOR BODY & ENGINE TYPE */

    printf( "%s%c%c%c%c%s\n", "SERIAL  : ", vin[13], vin[14], vin[15], vin[16], "<BR>" );

    printf( "%s\n", "*************************************************<BR>" );
    return 0;
#else
    printf( "%s\n", ".........1981-1995 928" );
    printf( "%s\n", "*************************************************" );
    printf( "%s%s\n", "VIN     : ", vin );

    if( vin[3] == 'Z' )
    {
	printf( "%s\n", "MODEL   : EURO " );
    }
    else
    {
	printf( "%s\n", "MODEL   : ROW " );
	printf( "%s%c%c%c\n", "VDS     : ", vin[3], vin[4], vin[5] );
    }

    if( (vin[6]!='9') || (vin[7]!='2') )
    {
	printf( "%s\n", "...OOPS!! FOUND AN INVALID VIN!" );
	return 0;
    }
    else
    {
	printf( "%s\n", "MODEL   : 928 " );
    }

    if( (vin[9]=='I') || (vin[9]=='O') || (vin[9]=='Q'))
    {
	printf( "%s\n", "...OOPS!! FOUND AN INVALID VIN!" );
	return 0;
    }
    else
    {
	check_year( vin[9] );
    }

    printf( "%s\n", "ORIGIN  : STUTTGART" );
    
    /* TODO : READ vin[12] AS CODE FOR BODY & ENGINE TYPE */

    printf( "%s%c%c%c%c\n", "SERIAL  : ", vin[13], vin[14], vin[15], vin[16] );

    printf( "%s\n", "*************************************************" );
    printf( "\n\n" );
    return 0;
#endif
}

//--------------------------------------PRE81VIN
// PRE81VIN
//----------------------------------------------
int pre81vin( char *vin )
{
#ifdef _928LIB_
    printf( "%s\n", ".........PRE-1981 928<BR>" );

    printf( "%s\n", "*************************************************<BR>" );
    printf( "%s%s%s\n", "VIN     : ", vin, "<BR>" );
    printf( "%s\n", "MODEL   : 928 <BR>" );
    if( vin[3] == '9' )
    {
	printf( "%s\n", "YEAR    : 1979<BR>" );
    }
    else
    {
	printf( "%s\n", "YEAR    : 1980<BR>" );
    }

    if( vin[4] == '1' )
    {
	printf( "%s\n", "ENGINE  : 4.5LTR [EURO]<BR>" );
    }
    else if( vin[4] == '2' )
    {
	printf( "%s\n", "ENGINE  : 4.5LTR [US/JAPAN]<BR>" );
    }
    else 
    {
	printf( "%s\n", "ENGINE  : 4.7LTR [EURO]<BR>" );
    }

    printf( "%s\n", "BODY    : COUPE<BR>" );
    printf( "%s%c%c%c%c%s\n", "SERIAL  : ", vin[6], vin[7], vin[8], vin[9], "<BR>" );
    printf( "%s\n", "*************************************************<BR>" );
#else
    printf( "%s\n", ".........PRE-1981 928" );

    printf( "%s\n", "*************************************************" );
    printf( "%s%s\n", "VIN     : ", vin );
    printf( "%s\n", "MODEL   : 928 " );
    if( vin[3] == '9' )
    {
	printf( "%s\n", "YEAR    : 1979" );
    }
    else
    {
	printf( "%s\n", "YEAR    : 1980" );
    }

    if( vin[4] == '1' )
    {
	printf( "%s\n", "ENGINE  : 4.5LTR [EURO]" );
    }
    else if( vin[4] == '2' )
    {
	printf( "%s\n", "ENGINE  : 4.5LTR [US/JAPAN]" );
    }
    else 
    {
	printf( "%s\n", "ENGINE  : 4.7LTR [EURO]" );
    }

    printf( "%s\n", "BODY    : COUPE" );
    printf( "%s%c%c%c%c\n", "SERIAL  : ", vin[6], vin[7], vin[8], vin[9] );
    printf( "%s\n", "*************************************************" );

    printf( "\n\n" );
#endif
    return 0;
}

//--------------------------------------DECODE_VIN
// DECODE_VIN
//------------------------------------------------
int decode_vin()
{
    /* VARS */
    char vin[20];
    char *p;
    /* ---- */
   

    printf( "%s", "...PLEASE ENTER THE VIN ==> " );
    fgets( vin, sizeof(vin), stdin );

    if( strlen( vin ) < 10 )
    {
	printf( "%s\n", "ERROR! : INVALID VIN!!" );
	return -1;
    }

    /* REMOVE THE NEWLINE */
    if( (p=strchr( vin, '\n' ) ) != NULL )
    {
	*p = '\0';
    }

    if( vin[0] == '9' )
    {
	pre81vin( vin );
    }
    else
    {
	post81vin( vin );
    }
    
    memset( vin, 0, sizeof( vin ) );

    return 0;
}

//--------------------------------------MENU
// MENU
//------------------------------------------
int menu()
{
    /* VARS */
    char choice[10];
    char *p;
    int quit = 0;
    /* ---- */

    while( !quit )
    {
	printf( "\n%s%s\n", "928CODES :: VERSION ", _928CODES_VERSION_ );
	printf( "%s\n", "============================================" );
	printf( "%s\n", "[0]   QUIT" );
	printf( "%s\n", "[1]   DECODE 928 VIN" );
	printf( "%s\n", "[2]   DECODE OPTION CODES" );
	printf( "%s\n", "[3]   DECODE PAINT CODES" );
	printf( "%s\n", "[4]   DUMP OPTION CODES" );
	printf( "%s\n", "[5]   HELP!" );
	printf( "%s\n", "============================================" );
	printf( "%s\n", "PLEASE SELECT AN OPTION" );
	printf( "%s", "====> " );
	
	fgets( choice, sizeof( choice ), stdin );

	/* REMOVE THE NEWLINE */
        if( (p=strchr( choice, '\n' ) ) != NULL )
        {
            *p = '\0';
        }
	
	printf( "\n" );
	
	if( choice[0] == '0' )
	{
	    printf( "%s\n", "...THANK YOU FOR USING 928CODES" );
	    memset( choice, 0, sizeof(choice) );
	    return 0;
	}
	else if( choice[0] == '1' )
	{
	    decode_vin();
	    memset( choice, 0, sizeof(choice) );
	}
	else if( choice[0] == '2' )
	{
	    decode_options();
	    memset( choice, 0, sizeof(choice) );
	}
	else if( choice[0] == '3' )
	{
	    decode_paint();
	    memset( choice, 0, sizeof(choice) );
	}
	else if( choice[0] == '4' )
	{
	    options_dump();
	    memset( choice, 0, sizeof(choice) );
	}
	else if( choice[0] == '5' )
	{
	    help();
	    memset( choice, 0, sizeof( choice ) );
	}
	else
	{
	    printf( "%s\n", "...INVALID SELECTION... TRY AGAIN" );
	    memset( choice, 0, sizeof(choice) );
	}
    }
    
    return 0;
}

#ifndef _928LIB_
//--------------------------------------MAIN
// MAIN
//------------------------------------------
int main( int argc, char **argv )
{
    /* VARS */
    int rtn = 0;
    /* ---- */

    rtn = menu();

    return rtn;
}
#endif
